<template>
  <section class="article-form-wrapper">
    <el-form
      :class="{ 'article-form': true, 'is-expand': isExpand }"
      :model="form"
      label-width="100px"
      inline
      @change="handleChange">
      <el-form-item label-width="120px" class="items-center">
        <template #label>
          <section class="flex items-center">
            <span>封面预览图：</span>
            <el-tooltip effect="dark" content="请先保存文章再上传封面预览图" placement="top">
              <i class="fas fa-circle-question text-[#909399]"></i>
            </el-tooltip>
          </section>
        </template>
        <upload
          :data="{ id: form.id }"
          :imageUrl="form.preview ?? ''"
          :api="uploadApi.uploadAritcleImg"
          :disabled="!form.id"
          height="100px"
          width="100px"
          @update:imageUrl="(val) => (form.preview = val)" />
      </el-form-item>
      <el-form-item label="标题：">
        <el-input
          v-model="form.title"
          placeholder="请输入"
          style="width: 300px"
          maxlength="50"
          clearable
          show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="描述：">
        <el-input
          v-model="form.description"
          type="textarea"
          autosize
          placeholder="请输入"
          style="width: 300px"
          maxlength="100"
          clearable
          show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="分类栏目：" class="flex items-center">
        <el-select v-model="form.categoryId" placeholder="请选择" style="width: 300px" clearable>
          <el-option v-for="item in categoryList" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div @click="isExpand = !isExpand" class="icon-wrapper">
      <i :class="isExpand ? 'fas fa-angles-up' : 'fas fa-angles-down'"></i>
    </div>
  </section>
</template>

<script setup lang="ts">
import categoryApi from '@/apis/category';
import uploadApi from '@/apis/upload';
import type { ArticleApiType } from '@/apis/article/namespace';
import type { CategoryApiType } from '@/apis/category/namespace';

const emit = defineEmits<{
  (e: 'update:modelValue', value: ArticleApiType.UpdateRequest): void;
}>();

const props = defineProps({
  modelValue: {
    type: Object as PropType<ArticleApiType.UpdateRequest>,
    required: true,
  },
});

// 展开/收缩
const isExpand = ref(false);

// 表单数据
const form = ref<ArticleApiType.UpdateRequest>({
  id: '',
  title: '',
  categoryId: '',
  preview: '',
});

// 栏目列表
const categoryList = ref<CategoryApiType.Category[]>([]);

const handleChange = () => {
  emit('update:modelValue', form.value);
};

// 获取栏目列表
const getCategoryList = async () => {
  categoryList.value = await categoryApi.list();
};

onMounted(() => {
  getCategoryList();
});

watch(
  () => props.modelValue,
  (value) => {
    form.value = value;
  },
  { immediate: true },
);
</script>

<style scoped lang="scss">
.article-form-wrapper {
  @apply relative mb-1 pt-8 px-4 border border-gray-300 rounded-md;
  transition: all 0.2s ease-in-out;
  .article-form {
    height: 100%;
    transition: all 0.3s;
  }
  .is-expand {
    height: 0;
    opacity: 0;
    transition: all 0.3s;
  }
  .icon-wrapper {
    @apply absolute right-2 top-1 text-[#909399];
    cursor: pointer;
  }
}
</style>
